Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Input: "()" Output: true
Input: "()[]{}" Output: true
Input: "(]" Output: false
Input: "([)]" Output: false
Input: "{[]}" Output: true
classSolution: defisValid(self, s: str) ->bool: iflen(s) %2!=0: returnFalsebrackets= {')': '(', '}': '{', ']': '['} stack= [] forcins: ifcinbracketsandstackandstack[-1] ==brackets[c]: stack.pop() else: stack.append(c) returnnotstack
classSolution: defisValid(self, s: str) ->bool: whiles.count("()") ors.count("[]") ors.count("{}"): s=s.replace("()", "") s=s.replace("[]", "") s=s.replace("{}", "") returnnots